<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Hash;
use App\Services\TtwlService;
use App\Services\WechatoffiaccountService;
use App\Services\WechatJsSdkService;
use EasyWeChat\Factory;
use EasyWeChat\Kernel\Messages\Text;
use EasyWeChat\Kernel\Messages\Image;
use EasyWeChat\Kernel\Messages\Media;
use EasyWeChat\Kernel\Messages\Video;
use Auth,Response,Log,Config;

class OffiaccountController extends Controller
{
    public function listenMsg(Request $request)
    {
    	$appid = empty($request->appid) ? "wx53ab6b46d8644ad2": $request->appid;
    	$config = TtwlService::getOffiaccount($appid);
		$app = Factory::officialAccount($config);

		if (!empty($request->echostr)) {
			$response = $app->server->serve();
			return $response;
		} else {
			$app->server->push(function ($message) {
			    switch ($message['MsgType']) {
			        case 'event':
			        	if ($message['Event'] == "CLICK") {
			        		return TtwlService::clickLists($message['EventKey']);
			        		return '菜单点击事件';
			        	} elseif ($message['Event'] == "SCAN") {
		        			// 公众号扫码事件
			        		return WechatoffiaccountService::officialUsersInfoLogin($message["FromUserName"],$message["Event"],$message["EventKey"]);
			        	} elseif ($message['Event'] == "subscribe") {
			        		// 关注公众号事件
			        		return WechatoffiaccountService::officialUsersInfoLogin($message["FromUserName"],$message["Event"],$message["EventKey"]);
			        	} else {
			        		return '收到事件消息';
			        	}
			            return '收到事件消息';
			            break;
		            case 'miniprogrampage':
			            return "🎊小程序信息获取成功🎊:\nAPPID:".$message['AppId']."\n小程序路径:".$message['PagePath'];
			            break;
			        case 'text':
			        	if (isset($message["bizmsgmenuid"])) {
			        		try {			        			
				        		$bizMsg = TtwlService::getBizmsgMenuId($message["bizmsgmenuid"]);
				        		if ($bizMsg["msgtype"] == "text") {
				        			$text = new Text($bizMsg["content"]);
				        			return $text;
				        		} elseif($bizMsg["msgtype"] == "image") {
				        			$image = new Image($bizMsg["media_id"]);
				        			return $image;
				        		} elseif ($bizMsg["msgtype"] == "meida") {
				        			$media = new Media($bizMsg["media_id"], $bizMsg["media_type"]);
				        			return $media;
				        		} elseif ($bizMsg["msgtype"] == "video") {
				        			$video = new Video($bizMsg["media_id"], [
								        'title' => $bizMsg["title"],
								        'description' => $bizMsg["description"],
								        'thumb_media_id' => $bizMsg["thumb_media_id"],
								    ]);
				        			return $media;
				        		}
				        	} catch (Exception $e) {
			        			Log::info($e);
			        		}
			            }
			            return TtwlService::cantUnderstand();
			            break;
			        case 'image':
			        	return TtwlService::cantUnderstand();
			            break;
			        case 'voice':
			        	return TtwlService::cantUnderstand();
			            break;
			        case 'video':
			        	return TtwlService::cantUnderstand();
			            break;
			        case 'location':
			        	return TtwlService::cantUnderstand();
			            break;
			        case 'link':
			        	return TtwlService::cantUnderstand();
			            break;
			        case 'file':
			        	return TtwlService::cantUnderstand();
			        // ... 其它消息
			        default:
			        	return TtwlService::cantUnderstand();
			            break;
			    }
			});
			$response = $app->server->serve();
			return $response;
		}
    }

    public function setOffiaccountMenu(Request $request)
    {
    	$appid = empty($request->appid) ? "wx53ab6b46d8644ad2": $request->appid;
    	$config = TtwlService::getOffiaccount($appid);
		$app = Factory::officialAccount($config);
    	$buttons = [
		    [
		        "name"		 => "演示案例",
		        "sub_button" => [
		            [
		                "type" => "click",
		                "name" => "查看演示视频",
		                "key"  => "demo"
		            ],
		        ],
		    ],
		    [
		        "name"       => "创建链接",
		        "sub_button" => [
		            [
		                "type" => "click",
		                "name" => "电脑创建链接",
		                "key"  => "help_create"
		            ],
		            [
		                "type" => "view",
		                "name" => "手机创建链接",
		                "url"  => "https://bab.moreqifu.cn/admin/links_lists/links_lists_add"
		            ],
		    //         [
		    //             "type"=>"miniprogram",
						// "name"=>"小程序创建链接",
						// "url"=>"https://mp.weixin.qq.com/s/rnRkEtl5N-rtQFG3zbFenQ",
						// "appid"=>"wx820e151d0905ab1a",
						// "pagepath"=>"pages/index/index"
		    //         ],
		            [
		                "type" => "view",
		                "name" => "天天外链官网",
		                "url"  => "https://www.tiantianwailian.com/"
		            ],
		        ],
		    ],
		    [
		        "name"       => "个人中心",
		        "sub_button" => [
		            [
		                "type" => "view",
		                "name" => "我的外链列表",
		                "url"  => "https://bab.moreqifu.cn/admin/links_lists/lists"
		            ],
		            [
		                "type" => "view",
		                "name" => "开通会员",
		                "url"  => "https://bab.moreqifu.cn/admin/prices/products_lists_pay"
		            ],
		            [
		                "type" => "view",
		                "name" => "邀请好友",
		                "url"  => "https://bab.moreqifu.cn/admin/links_lists/users_agent_lists"
		            ],
		            [
		                "type" => "view",
		                "name" => "联系客服",
		                "url" => "https://res.wailian1.cn/miniapp/links/20230706/3d821eb5ef6581544b2022b747aa62e2.jpg"
		            ],
		            [
		                "type" => "click",
		                "name" => "获取小程序路径",
		                "key" => "help_path"
		            ],
		        ],
		    ],
		];
		dump($buttons);
		$result = $app->menu->create($buttons);
		dd($result);
    }

    public function getOffiaccountMeida(Request $request)
    {
    	$appid = empty($request->appid) ? "wx53ab6b46d8644ad2": $request->appid;
    	$config = TtwlService::getOffiaccount($appid);
		$app = Factory::officialAccount($config);
    	$list = $app->material->list('image', 0, 20);
		dd($list);
    }

    public function wechatJssdk(Request $request)
    {
    	if (empty($request->sign)) return json_encode(["status"=>"error","msg"=>"参数错误!"]);
    	if ($request->sign != "So8769ne3612312gdsP862na921d96bs") return json_encode(["status"=>"error","msg"=>"参数错误!"]);
    	$weburl = $request->weburl;
    	$shareParamas = WechatJsSdkService::wechatJssdkParamas($weburl);
    	return json_encode($shareParamas);
    }
}
